package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.AppUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.di.component.DaggerSetServerAddressComponent;
import com.cj.mobile.iabp.mvp.contract.SetServerAddressContract;
import com.cj.mobile.iabp.mvp.presenter.SetServerAddressPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:设置服务器地址
 * <p>
 * Created on 2023/08/21 16:41
 *
 * @author 赤槿
 * module name is SetServerAddressActivity
 */
public class SetServerAddressActivity extends BaseActivity<SetServerAddressPresenter> implements SetServerAddressContract.View {
    /*------------------------------------------------控件信息------------------------------------------------*/
    @BindView(R.id.edit_setserveraddress_url)
    EditText editUrl;                                                                               // 地址

    @BindView(R.id.txvi_setserveraddress_tips1)
    TextView txviTips1;
    @BindView(R.id.txvi_setserveraddress_tips2)
    TextView txviTips2;

    @BindView(R.id.txvi_setserveraddress_url1)
    TextView txviRecommUrl1;
    @BindView(R.id.txvi_setserveraddress_url2)
    TextView txviRecommUrl2;
    /*------------------------------------------------业务区域------------------------------------------------*/
    @Inject
    AccountManager mAccountManager;

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mAccountManager = null;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSetServerAddressComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_set_server_address;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("设置服务器地址");

        // 设置当前使用的地址
        editUrl.setText(mAccountManager.getServiceUrl());


        // 初始化推荐地址
        txviTips1.setText("IABP正式");
        txviRecommUrl1.setText(Constant.IABP_SERVER_URL);

        txviTips2.setText("杨津路由侠");
        txviRecommUrl2.setText(Constant.ROUTING_MAN_URL_1);
    }

    @OnClick({
            R.id.btn_setserveraddress_save,                                                         // 保存
            R.id.txvi_setserveraddress_use1,
            R.id.txvi_setserveraddress_use2,
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_setserveraddress_save:                                                    // 保存
                String mUrl = editUrl.getText().toString();
                if (TextUtils.isEmpty(mUrl)) {
                    showMessage("请输入服务器地址！");
                    return;
                }

                onSave(mUrl);
                break;
            case R.id.txvi_setserveraddress_use1:                                                   // 推荐地址
                onSave(txviRecommUrl1.getText().toString());
                break;
            case R.id.txvi_setserveraddress_use2:
                onSave(txviRecommUrl2.getText().toString());
                break;
        }
    }

    /**
     * 保存地址
     */
    private void onSave(String url) {
        mAccountManager.setServiceUrl(url);
        AppUtils.relaunchApp(true);
    }

    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}